home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5526 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.4 KB  |  122 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: pInt = new int [987654321L], what will ha
  5. Message-ID: <marnoldDMA3n0.GqG@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <Pine.SOL.3.91.960203215559.25805E-100000@hamlet.uncg.edu> <31146e4c.197257088@nntp.ix.netcom.com> <Pine.SOL.3.91.960204100529.12786A-100000@hamlet.uncg.edu>
  8. Date: Mon, 5 Feb 1996 01:18:36 GMT
  9. Sender: marnold@netcom13.netcom.com
  10.  
  11. "QIAN . ZHONG" <q_zhong@hamlet.uncg.edu> writes:
  12.  
  13.  
  14. >On Sun, 4 Feb 1996, Mike Rubenstein wrote:
  15.  
  16. >> "QIAN . ZHONG" <q_zhong@hamlet.uncg.edu> wrote:
  17. >> 
  18. >> > 
  19. >> > Hi, folks:
  20. >> > 
  21. >> > Here is a question about new, the following is my code:
  22. >> > 
  23. >> > int main(void)
  24. >> > {
  25. >> >     int *pInt;
  26. >> >     long Block = 987654321L;
  27. >> > 
  28. >> >     pInt = new int[Block];
  29. >> >     if(pInt == NULL) dosomething();
  30. >> > }
  31. >> > 
  32. >> > My problem is when I compile the program for DOS under large memory 
  33. >> > model, above new nerve return NULL, I guess compiler convert
  34. >> > 
  35. >> > new int[987654321L] --- convert --> new int[ ACONST]
  36. >> > where ACONST = 0xffff , or ACONST = 0x7fff, am I right ?
  37. >> > Is this a DOS thing ? or a C++ thing ? Is this portable ?
  38. >> 
  39. >> More likely the system just doesn't have 987 meg free memory, so it
  40. >> can't do the allocation.  Newer compilers will throw an exception, but
  41. >> older ones simply return the null pointer.
  42. >> 
  43. >Hi, Michael:
  44.  
  45. >Thanks for your message, my real problem is the compiler will not return a
  46. >null pointer, even if I set_new_handler(0);
  47.  
  48. >I am confused. Please help me again. I use BC++ 4.0.
  49.  
  50. Your problem is related to compiling for DOS.  Most compilers use make
  51. size_t equivalent to a int.  DOS comilers usually make ints 16-bit
  52. quantities and longs 32-bit quantities.
  53.  
  54. Since new is prototyped as follows...
  55.  
  56.    void* operator new(size_t size);
  57.  
  58. ...it follows that you can only allocate chunks of memory whose size can
  59. be expressed with 16-bits using a DOS C++ compiler.
  60.  
  61. So, when you write...
  62.  
  63.    new int[BIG_NUMBER];
  64.  
  65. ...where BIG_NUMBER is a value which cannot be expressed in 16-bits and
  66. passed to new as a size_t (a 16-bit int, remember?), the compiler will
  67. perform an implicit conversion for you (often with an accompanying 
  68. warning).  In fact, compiling your sample code above, Borland C++ 4.52
  69. issues the following warning...
  70.  
  71. Warning CRAP.CPP 10: Conversion may lose significant digits in function main()
  72.  
  73. This warning means that the 32-bits required to represent 987654321 are
  74. being truncated to 16.  New will be passed 26801 (that's 897654321 with it's
  75. top 16 bits missing).  This is a perfectly reasonable value for the 
  76. tyipcally 16-bit memory management routines included with DOS compilers
  77. to allocate.  Thus, your code above will probably always succeed.  You're
  78. only asking new for 27K or so.
  79.  
  80. If you wish to allocate blocks of memory larger than 64K (16-bits allows you
  81. to express numbers up to 65535, or 64K, right?), in a DOS environment, most 
  82. DOS compilers will let you use "huge" pointers, along with accomanying "huge"
  83. memory allocations, in which 32-bit numbers can be used.
  84.  
  85. For example, your sample program can be modified to do what you really
  86. wanted it to do under borland C++ 4.52...
  87.  
  88. int main(void)
  89. {
  90.  int huge* pInt;
  91.  long Block = 8000000L;  // see below
  92.  
  93.  pInt = new huge int[Block];
  94.  if(pInt == 0)
  95.     dosomething();
  96.  
  97.  return 0;
  98. }
  99.  
  100. I changed the block size to about 8 megabytes instead of over 987 megabytes,
  101. which was a bit unreasonable.  Now, this program works properly.  However,
  102. it now uses the non-standard keyword "huge" (common to most DOS compilers,
  103. tho).
  104.  
  105. And, it was working properly before, except you didn't understand what was
  106. happening to your 32-bit 987654321 block size when passed to a 16-bit new.
  107.  
  108. Had you heeded the compiler warning (I do hope you have all warnings 
  109. enabled), you probably could have tracked down this problem yourself 
  110. instead of posting it to the world.
  111.  
  112.  
  113. Regards,
  114. -------------------------------------------------------------------------
  115. Matt Arnold                       |        | ||| | |||| |  | | || ||
  116. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  117. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  118. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  119. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  120. -------------------------------------------------------------------------
  121.